﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using dbPoll_Application.Models;
using System.Web.Mvc;

namespace dbPoll_Application.ViewModels
{
    public class ParticipantFieldViewModel
    {
        public String email;
        public IEnumerable<SelectListItem> bulk;
        public Poll poll;
        public String message;
        public ICollection<Dictionary<String,String>> fieldValues;
        public IList<Participant> participants;
        public ParticipantGroup group;
        public IList<Entity> entities;
        public IList<FieldsTemplate> templates;
        public int selectedTemplateID;
        public bool hasdems;
        public bool hasCompare;
        public IList<bool> hasdem;
        public IList<bool> hascom;
        public ParticipantType generic = ParticipantType.Generic;
        public ParticipantType web = ParticipantType.Web;
        public ParticipantType keypad = ParticipantType.Keypad;

        public ParticipantFieldViewModel(Poll poll)
        {
            this.poll = poll;
            if (poll.isGroup == false)
                this.fieldValues = ParticipantRepository.GetParticipantValuesByPollID(poll.pollID).OrderBy(first=>first["Device ID"])
                    .OrderBy(first => first["First Name"]).OrderBy(first => first["Last Name"]).ToList();
            else
            {
                this.fieldValues = ParticipantRepository.GetGroupValuesByPollID(poll.pollID).OrderBy(first => first["Name"]).ToList();
                hasdem = new List<bool>();
                hascom = new List<bool>();
                foreach (ParticipantField f in poll.participantFields)
                {
                    hasdem.Add(poll.questions.Where
                        (q => q.type == QuestionType.Demographic)
                        .Cast<DemographicQuestion>().Any(p => p.linkedField != null
                            && p.linkedField.fieldID == f.fieldID));
                    hascom.Add(poll.questions.Where(q => q.type == QuestionType.Demographic)
                        .Cast<DemographicQuestion>().Any(p => p.linkedField != null
                            && p.linkedField.fieldID == f.fieldID && p.compareWith != null));
                }
            }
            entities = getChildEntites();
            this.selectedTemplateID = -1;
            this.templates = FieldsTemplateRepository.GetFieldsTemplates();
            this.hasdems = poll.questions.Where
                    (q => q.type == QuestionType.Demographic)
                    .Cast<DemographicQuestion>().Any(p => p.linkedField != null);
            this.hasCompare = poll.questions.Where
                    (q => q.type == QuestionType.Demographic)
                    .Cast<DemographicQuestion>().Any(p => p.linkedField != null && p.compareWith != null);
        }

        public ParticipantFieldViewModel(Poll poll, IEnumerable<SelectListItem> users)
        {
            this.poll = poll;
            this.bulk = users;
        }

        public ParticipantFieldViewModel(Poll poll, ParticipantGroup group)
        {
            this.poll = poll;
            IList<Participant> part = new List<Participant>();
            part = poll.participants.Where(participant => participant.group != group).ToList<Participant>();
            this.participants=part;
            this.group = group;
            entities = getChildEntites();
            this.selectedTemplateID = -1;
            this.templates = FieldsTemplateRepository.GetFieldsTemplates();
        }

        public ParticipantType type (String type) 
        {
            if (type.Equals("Generic")) { return ParticipantType.Generic; }
            else if (type.Equals("Keypad")) { return ParticipantType.Keypad; }
            else if (type.Equals("Web")) { return ParticipantType.Web; }
            else return ParticipantType.None;
        }
        private IList<Entity> getChildEntites()
        {
            return EntityRepository.GetEndEntities(poll.pollID);
        }
    }
}